package com.sunny.blog.task;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.mail.MailUtil;
import com.sunny.blog.service.SmsSendService;
import com.sunny.blog.util.CommonUtil;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @ClassName: AutoBottleTask
 * @Description: 定时任务
 * @Author: sunt
 * @Date: 2019/12/11 16:05
 * @Version 1.0
 **/
@Component
@EnableScheduling //开启定时任务
@Slf4j
public class AutoBlogTask {

    /**
     * cron表达式
     *每隔5秒执行一次：5 * * * * ?
     *每隔1分钟执行一次：0 1 * * * ?
     *每天23点执行一次：0 0 23 * * ?
     *每天凌晨1点执行一次：0 0 1 * * ?
     *每月1号凌晨1点执行一次：0 0 1 1 * ?
     *每月最后一天23点执行一次：0 0 23 L * ?
     *每周星期天凌晨1点实行一次：0 0 1 ? * L
     *在26分、29分、33分执行一次：0 26,29,33 * * * ?
     *每天的0点、13点、18点、21点都执行一次：0 0 0,13,18,21 * * ?
    */

    @Autowired
    private SmsSendService smsSendService;

    private static final String HOST = "127.0.0.1";
    private static final String USER_NAME = "sunny_blog";
    private static final String PWD = "MeZ8JmVihW05GTkG";
    private static final String DB_NAME = "sunny_blog";
    private static final String BACK_PATH = "/usr/local/Blog/back/";

    @Scheduled(cron = "0 58 23 * * ?")
    public void autoBackDB() throws ApiException {

        try {
            String fileName = "SunnyBlog_DB_" + CommonUtil.getCurrentDayStr() + ".sql";
            log.info("执行定时任务备份数据start...");
            boolean res = CommonUtil.exportDatabaseTool(HOST, USER_NAME, PWD, BACK_PATH, fileName, DB_NAME);
            StringBuilder content = new StringBuilder();
            content.append(DateUtil.format(DateUtil.date(), DatePattern.NORM_DATETIME_PATTERN));
            content.append("\n\n");
            if(res) { //优先保证数据备份成功
                //将备份脚本以附件形式发送
                MailUtil.send("xxxxxx@126.com",  CommonUtil.getBeforeDay(-1) + "日MySQL生产数据库备份", "<h1>您好!附件为"+CommonUtil.getBeforeDay(-1)+"日MySQL生产数据库备份,请查收</h1>",
                        true, FileUtil.file(BACK_PATH + "SunnyBlog_DB_" + CommonUtil.getBeforeDay(-1) + ".sql"));
                content.append(CommonUtil.getBeforeDay(-1) + "日MySQL数据库备份成功,发送附件到xxxxx@126.com邮件成功，请知晓!");
                smsSendService.sendDingTextMsg(content.toString(),null,true);

                log.info("数据库成功备份,执行删除历史备份数据");
                CommonUtil.deleteFile(BACK_PATH + "SunnyBlog_DB_" + CommonUtil.getBeforeDay(-2) + ".sql");
            }else {
                log.info("------->数据备份失败...");
                smsSendService.sendDingTextMsg("数据库备份失败:",null,true);
            }
        } catch (Exception e) {
            log.error("----->备份数据系统异常:{}",e.getMessage());
            e.printStackTrace();
            smsSendService.sendDingTextMsg("数据库备份异常:" + ExceptionUtil.getSimpleMessage(e),null,true);
        }

    }

}
